from forward.api.admin.schemas.setting import SettingUserInSchema
from forward.utils.config import ConfigUtil
from forward.utils.urls import UrlUtil


class SettingUserService:
    """用户设置服务实现类"""

    async def detail(self) -> dict:
        """用户设置详情"""
        # 获取配置值并确保其为字符串类型
        default_avatar = await ConfigUtil.get_val("user", "defaultAvatar", "")
        if default_avatar is None:  # 验证是否为 None
            default_avatar = ""  # 如果是 None，则设置为空字符串
        return {
            "defaultAvatar": await UrlUtil.to_absolute_url(default_avatar),
        }

    async def save(self, user_in: SettingUserInSchema):
        """用户设置保存"""
        await ConfigUtil.set(
            "user",
            "defaultAvatar",
            await UrlUtil.to_relative_url(user_in.default_avatar),
        )

    @classmethod
    async def instance(cls):
        """实例化"""
        return cls()
